package cn.tedu.egarticle.controller;

import cn.tedu.egarticle.base.response.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Slf4j
@RequestMapping("v1/file/")
@RestController
@Api(tags = "02.文件上传模块")
public class UploadController {
    @ApiOperation("文件上传")
    @ApiOperationSupport(order = 10)
    @PostMapping("upload")
    public JsonResult upload(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        String houzhui = filename.substring(filename.lastIndexOf("."));
        filename = UUID.randomUUID() + houzhui;
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
        String datePath = sdf.format(new Date());
        String staticPath = "c:/files";
        File disFile = new File(staticPath + datePath);
        if (!disFile.exists()){
            disFile.mkdirs();
        }
        String filePath = staticPath + datePath + filename;
        file.transferTo(new File(filePath));



        return JsonResult.ok(datePath + filename);
    }
}
